<?php

class Draugiem{
	var $action;	//
	var $app; 		//aplikacijas API atslega (32 simboli)
	var $apikey;	//user API key (32 simboli)
	var $code;	//autorizācijas kods ko saņem pēc lietotāja 1 nosūtīšanas.
	var $email;	 	//lietotaja ievadita e-pasta adrese
	var $show;		//kādas vēstules rādīt?
	var $types;		//neobligāts parametrs, satur ar komatu atdalītus aktivitāšu tipus, ko vajadzīgs atlasīt. Norādot šo parametru, iespējams atfiltrēt datus par konkrētu tipu aktivitātēm, piemēram tikai jaunākās galerijas un dienasgrāmatu ierakstus.
	var $timestamp;	//neobligāts parametrs, satur datumu un laiku UNIX timestamp formātā. Norādot šo parametru, tiks rādītas tikai aktivitātes, kas jaunākas par norādīto laiku.
	var $draugiemURL = "http://api.draugiem.lv/xml/";	 	//draugiem URL
	var $first = "?";	//?
	var $and = "&";	 	//&
	var $link;
	var $onload;
	var $xml;	//xml return data
	var $data;	//array return data
	var $set = array();
	var $user = array();
	var $counters = array();
	var $activities = array();
	var $messages = array();

	function Draugiem(){
		$this -> setApp("aa6fa29686b3963907e81b54b45906fb");
	}
	
	function setApp($app){
		$this -> app 		= $app;
		$this -> set['app'] = true;
	}
	
	function getApp(){
		return $this -> app;
	}
	
	function setEmail($email){
		$this -> email 	= $email;
		$this -> set['email'] 	= true;
	}
	
	function getEmail(){
		return $this -> email;
	}
	
	function unsetEmail(){
		$this -> set['email'] = false;
	}
	
	function setApiKey($key){
		$this -> apikey 		= $key;
		$this -> set['apikey'] 	= true;
	}
	
	function getApiKey(){
		if($this -> set['apikey']){
			return $this -> apikey;
		}else{
			if(is_array($this->data)){
				return $this->apikey = $this->data[0]['elements'][0]['text'];
			}
		}
	}
	
	function setCode($code){
		$this -> code 	= $code;
		$this -> set['code']	= true;
	}
	
	function getCode(){
		if(!$this->set['code']){
			if(is_array($this->data)){
				$this->setCode($this->data[0]['elements'][0]['text']);
			}
		}
		return $this->code;
	}
	
	function setAction($action){
		$this -> action = $action;
		$this -> set['action'] = true;
	}
	
	function getAction(){
		return $this -> action;
	}
	
	function setShow($show){
		$this -> show = $show;
		$this -> set['show'] = true;
	}
	
	function getShow(){
		return $this -> show;
	}	
	
	function setTypes($types){
		$this -> types = $types;
		$this -> set['types'] = true;
	}
	
	function getTypes(){
		return $this -> types;
	}
	
	function setTimestamp($timestamp ){
		$this -> timestamp  = $timestamp ;
		$this -> set['timestamp '] = true;
	}
	
	function getTimestamp(){
		return $this -> timestamp ;
	}
	
	function createUrl(){
		$this->link  = $this->draugiemURL;
		if($this->set['app'])		{ $this->link .= $this->first."app=".$this->getApp(); 		}
		if($this->set['apikey'])	{ $this->link .= $this->and."apikey=".$this->getApiKey();	}
		if($this->set['action'])	{ $this->link .= $this->and."action=".$this->getAction();	}
		if($this->set['email'])		{ $this->link .= $this->and."email=".$this->getEmail(); 	}
		if($this->set['code'])		{ $this->link .= $this->and."code=".$this->getCode();		}
		if($this->set['show'])		{ $this->link .= $this->and."show=".$this->getShow();		}
		if($this->set['types'])		{ $this->link .= $this->and."types=".$this->getTypes();		}
		if($this->set['timestamp'])	{ $this->link .= $this->and."timestamp=".$this->getTimestamp();		}
		
		$this->set['link'] = true;
		return $this->link;
	}
		
	function getData($action){
		$this->setAction($action);
		$this->createUrl();
		if($this->set['link']){
			$this->xml = @file_get_contents($this->link);
		}
		$this->data = xml2array($this->xml);
		
		$this->set['data'] = true;
		return $this->data;
	}
	
	function getUser(){
		if($this->set['data']){
			$this->user[uid] = $this->data[0]['elements'][0]['elements'][0]['text'];
			$this->user[name] = $this->data[0]['elements'][0]['elements'][1]['text'];
			$this->user[surname] = $this->data[0]['elements'][0]['elements'][2]['text'];
			$this->user[email] = $this->data[0]['elements'][0]['elements'][3]['text'];
			$this->user[nick] = $this->data[0]['elements'][0]['elements'][4]['text'];
			$this->user[place] = $this->data[0]['elements'][0]['elements'][5]['text'];
			$this->user[age] = $this->data[0]['elements'][0]['elements'][6]['text'];
			$this->user[birthday] = $this->data[0]['elements'][0]['elements'][7]['text'];
			$this->user[sex] = $this->data[0]['elements'][0]['elements'][8]['text'];
			$this->user[language] = $this->data[0]['elements'][0]['elements'][9]['text'];
			$this->user[online] = $this->data[0]['elements'][0]['elements'][10]['text'];
			$this->user[img] = $this->data[0]['elements'][0]['elements'][11]['text'];
			return $this->user;
		}
	}
	
	function getCounters(){
		if($this->set['data']){
			$this->counters[friends] = $this->data[0]['elements'][0]['elements'][0]['text'];	//lietotāja draugu skaits
			$this->counters[visitors] = $this->data[0]['elements'][0]['elements'][1]['text'];	//profila apmeklējumu skaits
			$this->counters[unread] = $this->data[0]['elements'][0]['elements'][2]['text'];		//nelasīto vēstuļu skaits
			$this->counters[invites] = $this->data[0]['elements'][0]['elements'][3]['text'];	//jaunu draudzības aicinājumu skaits
			$this->counters[guestbook] = $this->data[0]['elements'][0]['elements'][4]['text'];	//jaunu viesu grāmatas ierakstu skaits
			$this->counters[blogcomments] = $this->data[0]['elements'][0]['elements'][5]['text'];//jaunu komentāru skaits dienasgrāmatā
			$this->counters[gallerycomments] = $this->data[0]['elements'][0]['elements'][6]['text'];//jaunu komentāru skaits galerijās
			$this->counters[galleryrating] = $this->data[0]['elements'][0]['elements'][7]['text'];//jaunu bilžu vērtējumu skaits galerijās
			$this->counters[newpictures] = $this->data[0]['elements'][0]['elements'][8]['text'];	//jaunu bilžu skaits galerijā
			$this->counters[ratings] = $this->data[0]['elements'][0]['elements'][9]['text'];	//jauni bildes vērtējumi TOP sadaļā
			return $this->counters;
		}
	}

	function getActivities(){
		//$this->activities
		if($this->set['data']){
			$this->data[0]['elements'][0]['elements'][0]['elements'][0]['elements'][0]['subject'];
		}
	}
	
	function getMessages(){
		//$this->messages
		if($this->set['data']){
			$this->data[0]['elements'][0]['elements'][0]['elements'][0]['elements'][0]['subject'];
		}
	}
	
	function showData(){
		if($this->set['data']){
			echo "<pre>";
			echo print_r($this->data);
			echo "</pre>";
		}
	}
	
}

//	LIETOJUMS

//$dr = NEW Draugiem();

/*
$dr -> setEmail("d_01@inbox.lv");
$dr -> getData("authorize");
$code = $dr -> getCode();
*/


/*
$dr -> unsetEmail();
$dr -> setCode("d12882f8a1f785455ff8");
$dr -> getData("authorize");
$ApiKey = $dr -> getApikey();
*/

//$dr -> setApiKey("f7772ba9bc421b2dcc8b7c5012782720");

/*
$dr -> getData("login");
$user = $dr -> getUser();
*/

/*
$dr -> getData("counters");
$counters = $dr -> getCounters();
print_r($counters);
*/
	
/* 
$dr -> setShow("friends");//my, friends, default = friends
//$dr -> setTypes();	// neobligāts parametrs, satur ar komatu atdalītus aktivitāšu tipus, ko vajadzīgs atlasīt. Norādot šo parametru, iespējams atfiltrēt datus par konkrētu tipu aktivitātēm, piemēram tikai jaunākās galerijas un dienasgrāmatu ierakstus.
//$dr -> setTimestamp ();	// neobligāts parametrs, satur datumu un laiku UNIX timestamp formātā. Norādot šo parametru, tiks rādītas tikai aktivitātes, kas jaunākas par norādīto laiku.
$dr -> getData("activities");
$dr -> showData(); 
 */


/* - negatavs
$dr -> setShow("unread");
$dr -> getData("messages");
$dr -> showData(); 
*/

	
	
	
//	FUNCTIONS
	
function xml2array($xml) {
	$xmlary = array();
	$reels = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s';
	$reattrs = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';
	preg_match_all($reels, $xml, $elements);
	foreach ($elements[1] as $ie => $xx) {
		$xmlary[$ie]["name"] = $elements[1][$ie];
		if ($attributes = trim($elements[2][$ie])) {
				preg_match_all($reattrs, $attributes, $att);
				foreach ($att[1] as $ia => $xx)
						$xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];
		}
		$cdend = strpos($elements[3][$ie], "<");
		if ($cdend > 0) {
				$xmlary[$ie]["text"] = substr($elements[3][$ie], 0, $cdend - 1);
		}
		if (preg_match($reels, $elements[3][$ie]))
				$xmlary[$ie]["elements"] = xml2array($elements[3][$ie]);
		else if ($elements[3][$ie]) {
				$xmlary[$ie]["text"] = $elements[3][$ie];
		}
	}
	return $xmlary;
}
?>

  